#include "renwu.h"
#include <QTimer>
#include <QKeyEvent>
#include <QPainter>
#include <QPixmap>

renwu::renwu(QWidget *parent) : QWidget(parent), currentImageIndex(0), isKeyPressedA(false)
{
    // 初始化定时器
    donghuashijian = new QTimer(this);
    connect(donghuashijian, &QTimer::timeout, this, &renwu::updateImage);


    imagePaths << ":/images/paodong.png" << ":/images/paodong2.png" << ":/images/paodong3.png"; //
}

renwu::~renwu() {
    delete donghuashijian;
}

void renwu::keyPressEvent(QKeyEvent *event) {
    if (event->key() == Qt::Key_A && !isKeyPressedA) {
        isKeyPressedA = true;

        if (!donghuashijian->isActive()) {
            donghuashijian->start(100);
        }
    }
}

void renwu::keyReleaseEvent(QKeyEvent *event) {
    if (event->key() == Qt::Key_A) {
        isKeyPressedA = false;

        donghuashijian->stop();
        // 还原当前显示的图片
        currentImageIndex = 0;
        update();  // 更新窗口
    }
}

void renwu::updateImage() {

    currentImageIndex = (currentImageIndex + 1) % imagePaths.size();  // 索引循环
    update();  // 更新窗口，触发重绘
}

void renwu::paintEvent(QPaintEvent *event) {
    QPainter painter(this);
    QPixmap currentPixmap(imagePaths[currentImageIndex]);
    painter.drawPixmap(0, 0, currentPixmap);
}
